﻿using Microsoft.EntityFrameworkCore;
using WidgetsDesign.API.DataBase;
using WidgetsDesign.API.Models;

namespace WidgetsDesign.API.Services
{
    public class EmployeeRepository : IEmployeeRepository
    {
        private readonly AppDbContext _context;

        public EmployeeRepository(AppDbContext context)
        {
            _context = context;
        }

        public async Task<IEnumerable<EmployeeModel>> GetEmployeesList(int pageIndex, int pageSize)
        {
            var query = (from h in _context.employees
                             /*where types.Contains(h.Type) && h.Length >= MinSize && h.Length <= MaxSize*/
                         select h).Skip(pageSize * (pageIndex - 1)).Take(pageSize);
            var filteredData = await query.ToListAsync();
            return filteredData;
        }
        public int GetEmployeesListTotal()
        {
            return (from h in _context.employees
                    select h).Count();
        }
        public void AddEmployees(EmployeeModel employeeModel)
        {
            if (employeeModel == null)
            {
                throw new ArgumentNullException(nameof(employeeModel));
            }
            _context.employees.Add(employeeModel);
        }
        public async Task<bool> SaveAsync()
        {
            return (await _context.SaveChangesAsync() >= 0);
        }

        public Task<EmployeeModel> GetEmployeesListAsync(string id)
        {
            return null;
        }

        public void UpdateEmployees(EmployeeModel model)
        {
            var query = from h in _context.employees where h.EmployeeId == model.EmployeeId select h;
            var info = query.FirstOrDefault();
            if (info != null)
            {
                info.BirthDate = model.BirthDate;
                info.Gender = model.Gender;
                info.Phone = model.Phone;
                info.Email = model.Email;
                info.HireDate = model.HireDate;
                info.Department = model.Department;
                info.Remarks = model.Remarks;
                info.WorkExperience = model.WorkExperience;
                info.EmergencyContactPhone = model.EmergencyContactPhone;
                info.EmergencyContactName = model.EmergencyContactName;
                info.Photo = model.Photo;
                info.Position = model.Position;
                info.Name = model.Name;
            }
        }

        public bool DeleteEmployees(string id)
        {
            var query = from h in _context.employees where h.EmployeeId == id select h;
            var info = query.FirstOrDefault();
            if (info != null)
            {
                _context.employees.Remove(info);
                return true;
            }
            else
            {
                return false;
            }
        }
        public async Task<EmployeeModel> GetEmployees(string id)
        {
            var query = from h in _context.employees where h.EmployeeId == id select h;
            var info =  await query.FirstOrDefaultAsync();
            if(info != null)
            {
                return info;
            }
            return new EmployeeModel();
        }
    }
}
